﻿using Dapper;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TR.Domain.Entity;
using TR.Domain.Entity.BsItemType;
using TR.Domain.Repository.Bs_WareAreaRepository;
using TR.Domain.Repository.Bs_warehouseRepostiory;
using TR.Domain.Repository.sqlDto;

namespace TR.EFCore.Repository.Bs_WareAreaRepository
{
    public class Bs_WareAreaRepository : Ibs_WareAreaRepository
    {
        private readonly MyContext myContext;
        public Bs_WareAreaRepository(MyContext myContext)
        {
            this.myContext = myContext;
        }

        public Task<int> count()
        {
            throw new NotImplementedException();
        }

        public async Task<bool> WareAreaAdd(bs_Ware_Area warearea)
        {
            using (SqlConnection connection = new SqlConnection("Data Source=rm-cn-omn3o8dvy000aako.sqlserver.rds.aliyuncs.com,1433;Initial Catalog=TR;User ID=l_123;Password=Ff123456;Encrypt=False;"))
            {
                // 获取当前时间
                DateTime currentTime = DateTime.Today;

                string sql = $"INSERT INTO [dbo].[bs_Ware_Area]([area_code],[area_name],[area_cap],[area_duty_man],[area_state],[sort_code],[create_by],[create_time],[update_by],[update_time],[is_del]) " +
                             $"VALUES ('{warearea.area_code}','{warearea.area_name}','{warearea.area_cap}','admin','{warearea.area_state}','{warearea.sort_code}','admin','{warearea.create_time}','{warearea.update_by}','{warearea.update_time}',0)";

                return connection.Execute(sql) > 0 ? true : false;
            }
        }

        public async Task<bs_Ware_Area> WareAreaSelectid(int id)
        {
            using (SqlConnection connection = new SqlConnection("Data Source=rm-cn-omn3o8dvy000aako.sqlserver.rds.aliyuncs.com, 1433;Initial Catalog=TR;User ID=l_123;Password=Ff123456;Encrypt=False;"))
            {
                string sql = "Select * from [dbo].[bs_Ware_Area] where [id] = @id";
                // 使用参数化查询来避免 SQL 注入
                var result = await connection.QueryAsync<bs_Ware_Area>(sql, new { id = id });
                // 返回单个结果，如果有多个结果，这里可以根据实际需要进行处理
                return result.FirstOrDefault();
            }
        }

        public async Task<bool> WareAreaUDelect(int id)
        {
            using (SqlConnection connection = new SqlConnection("Data Source=rm-cn-omn3o8dvy000aako.sqlserver.rds.aliyuncs.com, 1433;Initial Catalog=TR;User ID=l_123;Password=Ff123456;Encrypt=False;"))
            {
                string sql = $"delete from bs_Ware_Area where [id] = '{id}'";
                return connection.Execute(sql) > 0 ? true : false;
            }
        }

        public async Task<bool> WareAreaUpdate(bs_Ware_Area uAd)
        {
            using (SqlConnection connection = new SqlConnection("Data Source=rm-cn-omn3o8dvy000aako.sqlserver.rds.aliyuncs.com, 1433;Initial Catalog=TR;User ID=l_123;Password=Ff123456;Encrypt=False;"))
            {
                string sql = $"Update [dbo].[bs_Ware_Area] Set [area_code] ='{uAd.area_code}' ,[area_name]='{uAd.area_name}',[area_cap]='{uAd.area_cap}',[sort_code]='{uAd.sort_code}',[area_state]='{uAd.area_state}',[area_duty_man]='{uAd.area_duty_man}' where [id] ='{uAd.id}' ";
                return connection.Execute(sql) > 0 ? true : false;
            }
        }

        public async Task<FenYe<bs_Ware_Area>> WareArea_FenYeAsync(int pageSize, int currentPage, string str)
        {
            using (SqlConnection con = new SqlConnection("Data Source=rm-cn-omn3o8dvy000aako.sqlserver.rds.aliyuncs.com,1433;Initial Catalog=TR;User ID=l_123;Password=Ff123456;Encrypt=False;"))
            {
                DynamicParameters dynamicParameters = new DynamicParameters();
                dynamicParameters.Add("@pageSize", pageSize);
                dynamicParameters.Add("@keyName", "id");
                dynamicParameters.Add("@tableName", "bs_Ware_Area");
                dynamicParameters.Add("@currentPage", currentPage);
                dynamicParameters.Add("@where", str);
                dynamicParameters.Add("@rows", direction: ParameterDirection.Output, dbType: DbType.Int32);
                string sql = "exec [dbo].[procFenYe_Where] @pageSize, @keyName, @tableName, @currentPage, @where, @rows out";
                FenYe<bs_Ware_Area> fenYe = new FenYe<bs_Ware_Area>();
                fenYe.List = await con.QueryAsync<bs_Ware_Area>(sql, dynamicParameters);
                fenYe.Rows = dynamicParameters.Get<int>("rows");
                return fenYe;
            }
        }
        //修改

    }
}
